<?php namespace Admin\Model;
/**
 * 商品属性管理模型
 */
class GoodsAttr extends \Hdphp\Model\Model{
	protected $table = 'shop_goods_attr';
	/**
	 * 添加属性
	 * 使用$_POST中的数据添加
	 * @param int $goods_id [商品ID]
	 */
	public function addGoodsAttr($goods_id)
	{
		//最终插入到商品属性表的数据
		$insertData=array();
		foreach($_POST['attr_id'] as $k=>$v)
		{
			//属性值为空时不处理
			$_POST['attr_value'][$k] = trim($_POST['attr_value'][$k]);
			if(empty($_POST['attr_value'][$k]))
			{
				continue;
			}
			$data=array();
			$data['goods_id']=$goods_id;
			$data['attr_value']=$_POST['attr_value'][$k];
			$data['attr_id']=$_POST['attr_id'][$k];
			$data['add_price']=(int)$_POST['add_price'][$k];
			$where=array(
				'goods_id'=>$goods_id,
				'and attr_value'=>$data['attr_value'],
				'and attr_id'=>$data['attr_id']
			);
			//原有商品属性
			if($good_attr_id = Db::table('shop_goods_attr')->where($where)->pluck('good_attr_id'))
			{
				$data['good_attr_id']=$good_attr_id;
			}
			$insertData[]=$data;
		}
// p($insertData);exit;
		Db::table('shop_goods_attr')->where('goods_id',$goods_id)->delete();

		foreach($insertData as $d)
		{
			$this->add($d);
		}
		return true;
	}



	/**
	 * 根据选择的类型获取商品的属性列表
	 * @param  int $type_id  [商品类型]
	 * @param  int $goods_id [商品]
	 * @return string 组合好的属性html表单
	 */
	public function getAttrList($type_id,$goods_id)
	{
		//所有属性
		$sql = "SELECT a.attr_id,a.attr_name,a.attr_type,a.show_type,a.attr_values,a.shop_type_id,ga.attr_value,ga.add_price,ga.goods_id
		 FROM hd_shop_attr a 
		LEFT JOIN
		((SELECT * FROM hd_shop_goods_attr WHERE goods_id=$goods_id) AS ga)
		ON a.attr_id =ga.attr_id
		WHERE a.shop_type_id=$type_id";
		$attrs = Db::select($sql);
		$html='';
		foreach($attrs as $k=>$a)
		{
			//加字表单
			if($a['attr_type']==1)
				{
					//普通属性
					$priceField='<input type="hidden" name="add_price[]" value="'.$a['add_price'].'">';	
				}
				else
				{
					if($k>0 && $attrs[$k-1]['attr_id']==$a['attr_id']){
$priceField='加价:<input type="text" name="add_price[]" value="'.$a['add_price'].'"> <span onclick="removeGoodsAttr(this)">[-]</span>';
					}else{
					//规格属性
					$priceField='加价:
				<input type="text" name="add_price[]" value="'.$a['add_price'].'"> <span onclick="addGoodsPrice(this)">[+]</span>';
				}
				}
			//组合属性的html表单
			switch ($a['show_type']) {
				case 1:
				# 文本框
				$field =<<<php
				<tr>
					<td width="150" align="right">{$a['attr_name']}</td>
					<td>
						<input type="hidden" name="attr_id[]" value="{$a['attr_id']}">
						<input type="text" name="attr_value[]" value="{$a['attr_value']}">
						$priceField
					</td>
				</tr><br/>
php;
				break;
				case 2:
					# 列表框
				$attrValue = preg_split('@\n@',$a['attr_values']);
				$options="";
				foreach($attrValue as $opt)
				{
					$opt = trim($opt);
					$selected=$opt==$a['attr_value']?"selected":'';
					$options.='<option value="'.$opt.'" '.$selected.'>'.$opt.'</option>';
				}
				
				$field=<<<php
				<tr>
					<td width="150" align="right">{$a['attr_name']}</td>
					<td>
						<input type="hidden" name="attr_id[]" value="{$a['attr_id']}">
						<select name="attr_value[]">
							{$options}
						</select>
						$priceField
					</td>
				</tr><br/>
php;
				break;
			}
			$html.=$field;
		}
		return $html;

	}
}